package com.ossjk.oa.attendance.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.attendance.entity.Atdtable;
import com.ossjk.oa.attendance.service.IAtdtableService;
import com.ossjk.oa.system.entity.Department;
import com.ossjk.oa.system.service.IDepartmentService;

/**
 * 前端控制器
 * 
 * @Author Datou
 * @Version 1.0, 2020-05-23
 * @See
 * @Since com.ossjk.oa.attendance.controller
 * @Description: TODO
 */
@RestController
@RequestMapping("/attendance/atdtable")
public class AtdtableController extends BaseController {
	@Autowired
	private IAtdtableService iAtdtableService;
	@Autowired
	private IDepartmentService iDepartmentService;

	@RequiresPermissions("00050040")
	@GetMapping("/listSelf")
	public Object listSelf(String year, String month) {
		Wrapper wrapper = Condition.create().isWhere(true).eq("a.isdelete", "1");
		String eid = getCurrnetUserId();
		wrapper.eq("a.eid", eid);
		if (!CommonUtil.isBlank(year)) {
			wrapper.eq("a.year", year);
		}
		if (!CommonUtil.isBlank(month)) {
			wrapper.eq("a.month", month);
		}
		return responseSuccess(iAtdtableService.selectVo(wrapper));
	}

	@RequiresPermissions("00050045")
	@GetMapping("/listDepartment")
	public Object listDepartment(String did, String year, String month, Page page) {
		Wrapper wrapper = Condition.create().isWhere(true).eq("a.isdelete", "1");
		if (!CommonUtil.isBlank(did)) {
			wrapper.eq("a.did", did);
		}
		if (!CommonUtil.isBlank(year)) {
			wrapper.eq("a.year", year);
		}
		if (!CommonUtil.isBlank(month)) {
			wrapper.eq("a.month", month);
		}
		page = iAtdtableService.selectVoPage(page, wrapper);
		return responseSuccess(page);
	}

	@RequiresPermissions("000500400000")
	@PutMapping("/confirmAtdTable")
	public Object confirmAtdTable() {
		String eid = getCurrnetUserId();
		Department department = iDepartmentService.selectByEid(eid);
		Wrapper wrapper = Condition.create().eq("eid", eid).eq("did", department.getId());
		Atdtable atdtable = iAtdtableService.selectOne(wrapper);
		atdtable.setIsConfirmed(new BigDecimal(1));
		if (iAtdtableService.updateById(atdtable)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

}
